/*jslint vars:true plusplus:true*/

//=========================================================
// Interactive Digital Media Diploma
// CUFDIG404A_R1: Introduction to Scripting
// Assignment 3
// Author:
// File: 
// Description:
//=========================================================

var audio = new Audio('audio/theme.ogg');
audio.play();
audio.loop = true;
var lives = 3;

function ScoreSystem(){ 
    var score = 0;
    var highscore = 0;
    var scoreelt = document.getElementById("score");
    var highscoreelt = document.getElementById("highscore");
    var scorecount = 20;
    
    this.update = function(){
        scorecount--;
        if (scorecount === 0){
            if (window.game.buggy.isAlive())
            {
                score += 1;
            }
            scorecount = 20;
                
        }
        scoreelt.innerHTML = score+" points";
    }
       
 
        
        
    this.reset = function(){
        if (score >= highscore){
          highscore = score;
        highscoreelt.innerHTML = highscore;
        }
          
        score = 0;
    }    
   
    this.HitUfo = function(){
        score += 100; 
    }    
}


window.game.scoresystem = new ScoreSystem();
window.game.addSubsystem(window.game.scoresystem);

function BuggyExplode(spriteSrc, jumpVelocity) {
    "use strict";
    var self = this;
    var startHeight = 200;
     var sprite = new Sprite(spriteSrc, function (sprite) {
        sprite.offsetX = -75;
        sprite.offsetY = -25;
    }, 151, 63);
    
    var entity = new Entity(sprite, 100, startHeight);
    
    entity.isVisible = false;
    
    var explosioncount = 20;
    var explosiontimer = 5;
    
    // update the explosion every frame
    this.update = function () {
        explosioncount--;
        if (explosioncount === 0){
            entity.incSprite();
            explosioncount = 20;
        }
        
        explosiontimer -= 1/window.game.frameRate;
        if (explosiontimer < 0)
        {
            entity.isVisible = false;
        }
        entity.x += -window.game.background.getBaseSpeed() / window.game.frameRate;
    };
    
    
  //
    this.start = function ( x,y,x_vel, y_vel) {
        
        entity.isVisible = true;
        
        explosiontimer = 5;
        
        
        
      //y_velocity = y_vel;
    //  x_velocity = x_vel;
        entity.x = x;
        entity.y = y;
    };
}

//update the display based on lives
function updateicons() {
    if (lives === 3) {
        var heart1 = document.getElementById("livesicon1");
        heart1.src = "images/heart 1.png";
        var heart2 = document.getElementById("livesicon2");
        heart2.src = "images/heart 1.png";
        var heart3 = document.getElementById("livesicon3");
        heart3.src = "images/heart 1.png";
    } 
    
    if (lives === 2) {
        var heart1 = document.getElementById("livesicon1");
        heart1.src = "images/heart 1.png";
        var heart2 = document.getElementById("livesicon2");
        heart2.src = "images/heart 1.png";
        var heart3 = document.getElementById("livesicon3");
        heart3.src = "images/broken heart.png";
    }
       if (lives === 1) {
        var heart1 = document.getElementById("livesicon1");
        heart1.src = "images/heart 1.png";
        var heart2 = document.getElementById("livesicon2");
        heart2.src = "images/broken heart.png";
        var heart3 = document.getElementById("livesicon3");
        heart3.src = "images/broken heart.png";
    }
       if (lives === 0) {
        var heart1 = document.getElementById("livesicon1");
        heart1.src = "images/broken heart.png";
        var heart2 = document.getElementById("livesicon2");
        heart2.src = "images/broken heart.png";
        var heart3 = document.getElementById("livesicon3");
        heart3.src = "images/broken heart.png";
    }
}

  var explosion = new BuggyExplode("images/explosion Spliced.png");
  window.game.addSubsystem(explosion);

function alive(buggy){
 
    if (!buggy.isAlive())
    {
        if (lives > 0) {
            buggy.setalive(true);
        
        }
        else {
            window.game.scoresystem.reset();
            window.game.checkpoints.reset();
            buggy.setalive(true);
            lives = 3;
            updateicons();
        }
    }
}

function die(buggy) {
    explosion.start(buggy.getX(), buggy.getY())
    buggy.setalive(false);  
    lives = lives -1;
    if (lives < 0){
        lives = 0;
    }
    
    updateicons();
}